package com.ittest.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.ittest.entity.FillData;
import com.ittest.entity.FillDataHour;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ReadeExcelTemplate {

    // 基于注解导出数据
    public static void exportDataByAnnotation(List<FillData> fillData) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
        String format = dateFormat.format(new Date());
        StringBuilder stringBuilder  = new StringBuilder();
        stringBuilder.append("'\'").append(format).append("点前24小时降雨量数据统计").append(".xlsx");
        // 文件输出位置
        File outPutFile = new File(stringBuilder.toString());
    	// 指定使用哪个注解Bean来生成Excel？
        ExcelWriter excelWriter = EasyExcel.write(outPutFile, FillData.class).excelType(ExcelTypeEnum.XLSX).build();
    	// 生成数据
        excelWriter.write(fillData, EasyExcelFactory.writerSheet(0).build());
        // 完成，写数据到本地
        System.out.println("已完成写入本地！");
        excelWriter.finish();
    }
    // 基于注解导出数据
    public static void exportDataByAnnotationHour(List<FillDataHour> fillData) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
        String format = dateFormat.format(new Date());
        StringBuilder stringBuilder  = new StringBuilder();
        stringBuilder.append("'\'").append(format).append("点前1小时降雨量数据统计").append(".xlsx");
        // 文件输出位置
        File outPutFile = new File(stringBuilder.toString());
        // 指定使用哪个注解Bean来生成Excel？
        ExcelWriter excelWriter = EasyExcel.write(outPutFile, FillDataHour.class).excelType(ExcelTypeEnum.XLSX).build();
        // 生成数据
        excelWriter.write(fillData, EasyExcelFactory.writerSheet(0).build());
        // 完成，写数据到本地
        System.out.println("已完成写入本地！");
        excelWriter.finish();
    }

}

